package com.ffmpeg;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.ffmpeg.databinding.ActivityMainBinding;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());


        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},2);

        binding.choose.setOnClickListener(view -> startActivityForResult(new Intent(Intent.ACTION_GET_CONTENT).addCategory(Intent.CATEGORY_OPENABLE).setType("*/*"),3));
        binding.exe.setOnClickListener(view1 -> {
            File file=new File(getFilesDir()+"/ffmpeg");
            if (file.exists()){
                if (file.canExecute())file.setExecutable(true);
                binding.tv.setText("");
                if (binding.edit.getText().toString().equals("")){
                    return;
                }
                try {
                    Process process = Runtime.getRuntime().exec(Utils.concat(new String[]{getFilesDir() + "/ffmpeg"},binding.edit.getText().toString().split(" ")));
                    BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                    String line;
                    do {
                        line = err.readLine();
                        if (line!=null){
                            binding.tv.append("\n"+line);
                        }
                    } while (line != null);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else {
                Toast.makeText(this, "文件不存在", Toast.LENGTH_SHORT).show();
//                Utils.downLoadFile("http://cdn1.cxoip.com/libs/AndroLua/assets/armeabi-v7a/ffmpeg", getFilesDir() + "/ffmpeg", new Utils.DownloadStatus() {
//                    @Override
//                    public void finish(String dir) {
//                        File file=new File(getFilesDir()+"/ffmpeg");
//                        file.setExecutable(true);
//                        execute();
//                    }
//                    @Override
//                    public void error(IOException e) {
//                        e.printStackTrace();
//                    }
//                });
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String path= ContentUriUtil.getPath(this, data.getData());
        try {
            File file=new File(getFilesDir()+"/ffmpeg");
            Utils.copyFile(new File(path),file);
            file.setExecutable(true);
            Toast.makeText(this, "ok", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}